#include "userservice.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QUuid>

userService::userService()
{
    this->userRepository = new userDAO();
    this->userRepository->Init();
}

QString userService::addUser(QMap<QString, QString> params){
    UserType* node = new UserType();
    node->SetId(params["id"].toInt());
    node->SetName(params["name"]);
    node->SetPassword(params["password"]);

    this->userRepository->AddNode(node);
    this->userRepository->Save();

    QJsonObject obj;
    obj.insert("logid", QUuid::createUuid().toString());
    obj.insert("code", 200);
    obj.insert("message", "success");
    QJsonObject data;
    obj.insert("data", data);

    return QString(QJsonDocument(obj).toJson(QJsonDocument::Compact));
}

QString userService::searchUser(QMap<QString, QString> params){
    UserType* node = new UserType();
    node->SetId(params["id"].toInt());
    node->SetName(params["name"]);
    node->SetPassword(params["password"]);

    this->userRepository->AddNode(node);
    this->userRepository->Save();

    QJsonObject obj;
    obj.insert("logid", QUuid::createUuid().toString());
    obj.insert("code", 200);
    obj.insert("message", "success");
    QJsonObject data;
    obj.insert("data", data);

    return QString(QJsonDocument(obj).toJson(QJsonDocument::Compact));
}

QString userService::deleteUser(QMap<QString, QString> params){
    UserType* node = new UserType();
    node->SetId(params["id"].toInt());
    this->userRepository->DeleteNode(node->GetId());
    this->userRepository->Save();

    QJsonObject obj;
    obj.insert("logid", QUuid::createUuid().toString());
    obj.insert("code", 200);
    obj.insert("message", "success");
    QJsonObject data;
    obj.insert("data", data);

    return QString(QJsonDocument(obj).toJson(QJsonDocument::Compact));
}

QString userService::updateUser(QMap<QString, QString> params){
    UserType* node = new UserType();
    node->SetId(params["id"].toInt());
    node->SetName(params["name"]);
    node->SetPassword(params["password"]);

    this->userRepository->UpdateNode(node);
    this->userRepository->Save();

    QJsonObject obj;
    obj.insert("logid", QUuid::createUuid().toString());
    obj.insert("code", 200);
    obj.insert("message", "success");
    QJsonObject data;
    obj.insert("data", data);

    return QString(QJsonDocument(obj).toJson(QJsonDocument::Compact));
}
